﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dotnet_framework
{
    class Program
    {
        // 编号(对应的电表的地址编号)
        // 本次电量的记录时间
        // 当前电量
        public static void Main(string[] args)
        {
            //快捷键大全
            //先同时按快捷键ctrl+k，在同时按ctrl+d，之后代码自动格式化
            //

            //            默认注释快捷组合键
            //注释：
            //Ctrl + K → 选定需要注释的代码段 →Ctrl + C

            //取消注释：
            //Ctrl + K → 选定需要取消注释的代码段 →Ctrl + U

            // ctrl+L ->剪切当删除用
            //test_insert();
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(3);
            list.Insert(list.Count-1, 2);
            Console.ReadKey();
        }
        static void test_insert()
        {
            for (int i = 0; i < 10; i++)
            {
                String name = "张三" + i;
                int age = 11 + i;
                new Program().NonQuery(string.Format("insert into st_user(name,age) values('{0}',{1})", name, age));

            }
        }
        static void my_query()
        {
            DataSet dd = new Program().Query("select * from st_user");
            DataRowCollection drc = dd.Tables[0].Rows;
            int count = drc.Count;
            for (int i = 0; i < count; i++)
            {
                string name = (string)drc[i]["name"];
                int age = (int)drc[i]["age"];
                Console.WriteLine("name:{0},age:{1}", name, age);
            }
        }
        static void test01()
        {
            int[] arr = { 1, 2 };
            changeValue(arr);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < arr.Length; i++)
            {
                sb.Append(arr[i] + ",");
            }
            Console.WriteLine(sb);

        }
        static void changeValue(int[] arr)
        {
            arr[1] = 3;
        }
        public string connString = "Data Source=127.0.0.1;Initial Catalog=hohd;Integrated Security=TRUE;User ID = sa;Pwd = 123456";
        //创建连接对象的变量  
        public SqlConnection conn;
        // 执行对数据表中数据的增加、删除、修改操作  
        public int NonQuery(string sql)
        {
            conn = new SqlConnection(connString);
            int a = -1;
            try
            {
                conn.Open();  //打开数据库  
                SqlCommand cmd = new SqlCommand(sql, conn);
                a = cmd.ExecuteNonQuery();
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();    //关闭数据库  
                }
            }
            return a;

        }
        // 执行对数据表中数据的查询操作  
        public DataSet Query(string sql)
        {
            conn = new SqlConnection(connString);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();      //打开数据库  
                SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
                adp.Fill(ds);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();      //关闭数据库  
            }
            return ds;
        }
    }
}
